package cn.lzwei.bilimall.product.service.impl;

import cn.lzwei.bilimall.product.entity.BrandEntity;
import cn.lzwei.bilimall.product.entity.CategoryEntity;
import cn.lzwei.bilimall.product.service.BrandService;
import cn.lzwei.bilimall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lzwei.common.utils.PageUtils;
import cn.lzwei.common.utils.Query;

import cn.lzwei.bilimall.product.dao.CategoryBrandRelationDao;
import cn.lzwei.bilimall.product.entity.CategoryBrandRelationEntity;
import cn.lzwei.bilimall.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    BrandService brandService;
    @Resource
    CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存品牌的分类信息：添加冗余字段 分类名+品牌名
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //1.获取品牌、分类信息
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brand = brandService.getById(brandId);
        CategoryEntity category = categoryService.getById(catelogId);
        //2.添加冗余字段 分类名+品牌名
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());
        //3.保存关联信息
        this.save(categoryBrandRelation);
    }

    /**
     * 分类修改：并更新在其他表中的冗余字段
     */
    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId,name);
    }

    /**
     * 品牌修改：并更新在其他表中的冗余字段
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandName(name);
        baseMapper.update(
                categoryBrandRelationEntity,
                new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId)
        );
    }
    /**
     * 查询分类下的可供选择品牌，用于用户商品发布的基本信息填写
     * @return
     */
    @Override
    public List<CategoryBrandRelationEntity> getCategoryRelationBrands(Long catId) {
        //通过分类id关联查询出品牌信息
        List<CategoryBrandRelationEntity> entities = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        return entities;
    }

}